<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>小黑记事本</title>
  </head>
  <body style="width: 100%; margin: 0; padding: 0">
    <div
      id="app"
      style="
        width: 300px;
        height: auto;
        border: 1px solid #000;
        margin: 20px auto;
        padding: 20px 0;
      "
    >
      <h1 style="text-align: center; color: #ca6358">小黑记事本</h1>
      <div style="text-align: center">
        <span><input type="text" v-model="task" /></span>
        <button @click="addTask(task)">添加任务</button>
        <div
          style="
            text-align: left;
            padding: 0 20px;
            width: 100%;
            display: flex;
            flex-wrap: wrap;
          "
          v-for="(item,index) in tasksList"
          :key="item.id"
        >
          <span>{{ index+1 }}.</span>&nbsp;
          <span>{{ item.like }}</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <span style="flex: 1" @click="del(item.id)">X</span>
        </div>
        <div v-show="tasksList.length > 0" style="margin-bottom: 3px">
          <span>合计：{{ tasksList.length}}</span
          >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <span @click="clear">清空任务</span>
        </div>
      </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

    <script>
      const app = new Vue({
        el: "#app",
        data: {
          task: "",
          tasksList: [
            { id: 1, like: "唱歌" },
            { id: 2, like: "游泳" },
            { id: 3, like: "舞蹈" },
          ],
        },
        methods: {
          addTask(r) {
            if (r.trim() === "") {
              alert("请输入任务");
              return;
            }
            this.tasksList.unshift({
                id:+new Date(),
                like:r
            });
            this.task = "";
          },
          del(id) {
            console.log(id);

            this.tasksList = this.tasksList.filter((item) => item.id !== id);
          },
          clear() {
            this.tasksList = [];
          },
        },
      });
    </script>
  </body>
</html>
